package jsu.blogger.mapper;


import java.util.List;
import jsu.blogger.pojo.BlogPostDetails;
import jsu.blogger.pojo.Post;
import org.apache.ibatis.annotations.*;

@Mapper
public interface PostMapper {

    @Update("UPDATE blog_post_details SET read_count = read_count + 1 WHERE post_id = #{postId}")
    int incrementReadCount(@Param("postId") Long postId);
    // 使用 @Delete 注解删除数据
    @Delete("DELETE FROM posts WHERE user_id = #{userId} AND post_id = #{postId} AND post_type = 0")
    int deleteBlog(@Param("userId") Long userId, @Param("postId") Long postId);
    /**
     * 增加指定帖子的不喜欢数
     *
     * @param postId 帖子ID
     */
    @Update("UPDATE blog_post_details SET dislike_count = dislike_count + 1 WHERE post_id = #{postId}")
    int increaseDislikeCount(@Param("postId") Long postId);

    /**
     * 减少指定帖子的 不喜欢数，前提是 dislike_count > 0
     *
     * @param postId 帖子ID
     */
    @Update("UPDATE blog_post_details SET dislike_count = dislike_count - 1 " +
            "WHERE post_id = #{postId} AND dislike_count > 0")
    int decreaseDislikeCount(@Param("postId") Long postId);

    /**
     * 增加帖子喜欢数
     *
     * @param postId 帖子ID
     * @return 返回影响的行数
     */
    @Update("UPDATE posts SET likes_count = likes_count + 1 WHERE post_id = #{postId}")
    int increaseLikesCount(@Param("postId") Long postId);

    /**
     * 减少帖子喜欢数，前提是 likes_count > 0
     *
     * @param postId 帖子ID
     * @return 返回影响的行数
     */
    @Update("UPDATE posts SET likes_count = likes_count - 1 " +
            "WHERE post_id = #{postId} AND likes_count > 0")
    int decreaseLikesCount(@Param("postId") Long postId);


    @Update("UPDATE blog_post_details SET favorite_count = favorite_count - 1 WHERE post_id = #{postId} AND favorite_count > 0")
    int decrementFavoriteCount(Long postId);

    @Update("UPDATE blog_post_details SET favorite_count = favorite_count + 1 WHERE post_id = #{postId}")
    int incrementFavoriteCount(Long postId);

    // 根据 postId 查询帖子内容
    @Select("SELECT content FROM posts WHERE post_id = #{postId}")
    String getContentByPostId(@Param("postId") Long postId);
    // 根据 post_id 删除关联的标签
    @Delete("DELETE FROM post_tags WHERE post_id = #{postId}")
    void deletePostTagsByPostId(Long postId);

    @Select("SELECT tag_id FROM tags WHERE tag_name = #{tagName}")
    Long getTagIdByName(String tagName);

    @Insert("INSERT INTO post_tags (post_id, tag_id) VALUES (#{postId}, #{tagId})")
    void insertPostTag(@Param("postId")Long postId,@Param("tagId") Long tagId);
    int insertPost(Post post);
    @Update("UPDATE posts SET content = #{content}, updated_at = CURRENT_TIMESTAMP WHERE post_id = #{postId}")
    void updatePostContent(@Param("postId")Long postId,@Param("content") String content);
    // 插入 blog_post_details 数据
    @Insert("INSERT INTO blog_post_details (post_id, read_count, share_count, favorite_count, dislike_count, average_read_time, blog_type, blog_describe, original_url) " +
            "VALUES (#{postId}, #{readCount}, #{shareCount}, #{favoriteCount}, #{dislikeCount}, #{averageReadTime}, #{blogType}, #{blogDescribe}, #{originalUrl})")
    void insertBlogPostDetails(BlogPostDetails blogPostDetails);
    //获取 处了内容的一切博客属性
    BlogPostDetails getBlogDetailApartContentById(@Param("postId") Long var1);

    /**
     * 根据 user_id 查询所有的 post_id
     *
     * @param userId 用户ID
     * @return 所有的 post_id 列表
     */
    List<Long> getPostIdsByUserIdWithPagination(@Param("userId") Long userId, @Param("offset")Long offset, @Param("count") Long count);

    //根据 标签名分页查询相关联的博客id
    List<Long> selectPostIdsWithTagNames(@Param("tagNames") List<String> var1, @Param("offset") Long var2, @Param("limit") Long var3);

    // 根据多个关键词查询帖子 ID，只检索标题
    List<Long> selectPostIdsByKeywordsList(@Param("keywords") List<String> keywords);

    //判断帖子是否存在 根据id和post_type
    int countPostsByPostIdAndPostType(@Param("postId") Long postId,@Param("userId") Long userId, @Param("postType") Integer postType);
    Post getPostTitleAndContentById(@Param("postId") Long postId, @Param("userId") Long userId,@Param("postType") Integer postType);

    @Select("SELECT p.user_id, p.post_id, p.title, p.created_at, substring(d.blog_describe,1,100) AS short_content, p.likes_count, p.comments_count " +
            "FROM blog_system_database.posts p " +
            "LEFT JOIN blog_system_database.blog_post_details d ON p.post_id = d.post_id " +
            "WHERE p.title LIKE CONCAT('%', #{searchContent}, '%') AND p.post_type = 0 " +
            "LIMIT #{begin}, #{pageSize}")

    @Results({
            @Result(property = "userId",column = "user_id"),
            @Result(property = "postId",column = "post_id"),
            @Result(property = "createdAt",column = "created_at"),
            @Result(property = "content",column = "short_content"),
            @Result(property = "likesCount",column = "likes_count"),
            @Result(property = "commentsCount",column = "comments_count")
    })
    List<Post> selectPostListPageBySearchContent(
            @Param("searchContent") String searchContent,
            @Param("begin") int begin,
            @Param("pageSize") int pageSize);

    @Select("select user_id as 'userId',post_id as 'postId',created_at as 'createdAt',title " +
            " from blog_system_database.posts " +
            "where user_id=#{userId} and post_type=#{postType} LIMIT #{begin}, #{pageSize}")
    List<Post> selectPoseListPageByUserId(
            @Param("userId") Long userId,
            @Param("begin") int begin,
            @Param("pageSize") int pageSize,
            @Param("postType") int postType
    );

    @Select("SELECT  user_id as 'userId',post_id as 'postId',created_at as 'createdAt',title ,content" +
            " FROM blog_system_database.posts ")
    List<Post> selectAllPost();
}